$(function () {
    var num;
    var register = new Register();
    var  username,pwd,phone,email;
    // 验证用户名
    $('.username').on('blur',function () {
        username = $('.username').val();
         num = register.checkUserName(username);
    })
    $('.username').on('focus',function () {
        $('.error1').html(" ");
    })
    //验证密码
    $('.password').on('blur',function () {
        pwd = $('.password').val();
        num += register.checkPassword(pwd);
    })
    $('.password').on('focus',function () {
        $('.error2').html(" ");
    })
    //验证手机号
    $('.phone').on('blur',function () {
        phone = $('.phone').val();
        num += register.checkPhone(phone);
    })
    $('.phone').on('focus',function () {
        $('.error3').html(" ");
    })
    //验证邮箱
    $('.email').on('blur',function () {
        email = $('.email').val();
        num += register.checkEmail(email);
    })
    $('.email').on('focus',function () {
        $('.error5').html(" ");
    })

    //为注册按钮添加事件
    $('.tijiao').on('click',function (e) {
        e.preventDefault();
        if(num==4){
            var user = {
                userName:username,
                passWord:pwd,
            }
            var userList = JSON.parse(localStorage.getItem('userList')) || [];
			userList.push(user);
            localStorage.setItem('userList',JSON.stringify(userList));
            num = 0;
            window.location.href="../../login.html";
        }
    })
})

var Register = function () {
    
}

Register.prototype = {
    checkUserName:function (username) {
        var regName = /[a-zA-Z]\w{4,16}/;
        if (username == '' || username.trim() == "") {
            $('.error1').html("*请输入用户名");
            return false;
        }else if(!regName.test(username)){
            $('.error1').html("*由英文字母和数字组成的4-16位字符，以字母开头");
            return false;
        }else{
            return 1;
        }
    },
    checkPassword:function (pwd) {
        var regPwd = /^\w{6,20}$/;
        if (pwd == "" || pwd.trim() == "") {
            $('.error2').html("*请输入密码");
            return false;
          } else if (!regPwd.test(pwd)) {
            $('.error2').html("*密码长度要在6到20之间");
            return false;
          } else {
            return 1;
          }
    },
    checkPhone:function (phone) {
        var regPhone = /^1\d{10}$/;
        if (phone == "" || phone.trim() == "") {
            $('.error3').html("*请输入手机号");
            // return false;
          } else if (!regPhone.test(phone)) {
            $('.error3').html("*手机号码格式错误");
            return false;
          } else {
            return 1;
          }
    },
    checkEmail:function (email) {
        var regEmail = /^\w+@\w+((\.\w+)+)$/;
        if (email == "" || email.trim() == "") {
            $('.error5').html("*请输入邮箱");
            return false;
          } else if (!regEmail.test(email)) {
            $('.error5').html("*邮箱账号@域名。如good@tom.com、whj@sina.com.cn");
            return false;
          } else {
            return 1;
          }
    }
}

